import java.io.File
import java.nio.file.Files
import java.nio.file.Paths
import java.nio.file.StandardCopyOption

class Tool() : Com_relation {

//    var commandMap: MutableMap<String, () -> Unit> = mutableMapOf(
//        "ls" to ::ls,
//        "pwd" to ::pwd,
//        "mkdir" to ::mkdir,
//        "cd" to ::cd,
//        "rm" to ::rm,
//        "cp" to ::cp,
//        "mv" to ::mv,
//        "rn" to ::rn
//    )
    var words :MutableList<String> = mutableListOf()
    var list:String=""
    override fun commandtoneed() :String{
        return list
    }
    override fun tooltoneed(words: MutableList<String>, list: String) {
        this.words = words
        this.list = list
    }
    override fun ls(){
        if(words.size==2&&words[1]=="-l"){
            val folderPath = list
            val folder = File(folderPath)
            if (folder.exists() && folder.isDirectory) {
                val files = folder.listFiles()
                if (files != null) {
                    for (file in files) {
                        println("${file.name}               ${file.length()}")
                    }
                    return
                } else {
                    println("当前文件夹没有文件或者无法访问")
                }
            } else {
                println("目录不存在或无法访问")
            }
        }
        val folderPath = if (words.size == 1) list else words[1]
        val folder = File(folderPath)
        if (folder.exists() && folder.isDirectory) {
            val files = folder.listFiles()
            if (files != null) {
                for (file in files) {
                    println(file.name)
                }
            } else {
                println("当前文件夹没有文件或者无法访问")
            }
        } else {
            println("目录不存在或无法访问")
        }
    }
    override fun pwd(){
        println(list)
    }
    override fun mkdir(){

        val newDir= File(list,words[1])
        if(newDir.exists()){
            println("当前文件夹已经存在")
        }else{
            val success=newDir.mkdir()
            if(success){
                println("${words[1]}创建成功")
            }else{
                println("${words[1]}创建失败")
            }
        }
    }
    override fun cd(){
        if(words.size==2&& File(words[1]).exists()&& File(words[1]).isDirectory)
        {
            list=words[1]
            return
        }
        if(words[1]==".."){
            for (i in  list.length-1 downTo 0)
            {
                if(list[i].toString()=="\\"){
                    var a=""
                    for (j in 0 until  i)
                    {
                        a=a+list[j]
                    }
                    list=a
                    return
                }
            }
        }
        if(words.size==2) {
            val l=list+"\\"+words[1]
            val folder = File(l)
            if(folder.exists() && folder.isDirectory){
                list=l
            }else{
                println("输入错误，请重新输入")
            }
        }
    }
    override fun rm(){
        if(words.size==2&& File(words[1]).exists()){
            val filePath = Paths.get(words[1])
            try {
                Files.delete(filePath)
                println("File deleted successfully")
            } catch (e: Exception) {
                println("Error deleting file: ${e.message}")
            }
        }
        try {
            Files.delete(Paths.get(list+"\\"+words[1]))
            println("File deleted successfully")
        } catch (e: Exception) {
            println("Error deleting file: ${e.message}")
        }
    }
    override fun cp(){
        val sourceFilePath = words[1]  // 替换为源文件的路径
        val destinationFilePath = words[2] // 替换为目标文件的路径

        try {
            val sourcePath = Paths.get(sourceFilePath)
            val destinationPath = Paths.get(destinationFilePath)
            Files.copy(sourcePath, destinationPath, StandardCopyOption.REPLACE_EXISTING)
            println("File copied successfully")
        } catch (e: Exception) {
            println("Error copying file: ${e.message}")
        }
    }
    override fun mv(){
        val sourceFilePath = words[1]  // 替换为源文件的路径
        val destinationFilePath = words[2] // 替换为目标文件的路径
        try {
            val sourcePath = Paths.get(sourceFilePath)
            val destinationPath = Paths.get(destinationFilePath)

            Files.move(sourcePath, destinationPath, StandardCopyOption.REPLACE_EXISTING)
            println("File moved successfully")
        } catch (e: Exception) {
            println("Error moving file: ${e.message}")
        }
    }
    override fun rn(){
        val oldFilePath = words[1] // 替换为源文件的路径
        val newFilePath = words[2]  // 替换为新文件的路径

        try {
            val sourcePath = Paths.get(oldFilePath)
            val destinationPath = Paths.get(newFilePath)

            Files.move(sourcePath, destinationPath, StandardCopyOption.REPLACE_EXISTING)
            println("File renamed successfully")
        } catch (e: Exception) {
            println("Error renaming file: ${e.message}")
        }
    }
}